<?php

namespace App\V3\V3_2_6\Controllers;

use App\V3\V3_2_6\Services\OverflowBreakageService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class OverflowBreakageController extends BaseController
{
    /**
     *新建报溢报损
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/5/20 10:39
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
//            'goods_info' =>'required|string',//商品信息  json array
            'proof_image' =>'string',//凭证图片(多图逗号分隔)
            'check_info' =>'required|string',//审批人信息 json array
            'type' =>'required|integer',//类型：0-报溢 1-报损
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail($info);
        }

        //报溢报损主单
        $mainInfo = [
            'main_id'=>$this->getMainUserId(),
            'user_id'=>$this->getUserId(),
            'type'=>$data['type'],
            'created_at'=>time(),
        ];

        $this->log('新建报溢报损', $data);

        $data['is_use_shelf'] = $this->isUseShelf();
        $data['is_use_batch'] = $this->getOutWay();

        return response()->json(
            OverflowBreakageService::add($mainInfo, $data)
        );
    }

    public function index(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'pageSize' =>'required|integer',//页面大小
            'currentPage' =>'required|integer',//当前页
            'type'=>[//类型：0-报溢 1-报损
                'required',
                Rule::In([0,1])
            ],
            'repeal_status'=>[//撤销状态：0-未撤销 1-已撤销 2-全部
                'required',
                Rule::In([0,1,2])
            ],
            'status'=>[//	审批状态(即入库状态):0-待审批 1-通过 2-拒绝 3-全部
                'required',
                Rule::In([0,1,2,3])
            ],
        ], [
            'pageSize.required' => '参数`页面大小`缺失',
            'currentPage.required' => '参数`当前页`缺失',
            'type.required' => '参数`类型`缺失',
            'type.In' => '参数`类型`不合法',
            'repeal_status.required' => '参数`撤销状态`缺失',
            'repeal_status.In' => '参数`撤销状态`不合法',
            'status.required' => '参数`审批状态`缺失',
            'status.In' => '参数`审批状态`不合法',
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        if(isset($request->start_time)) {
            $timeValidator = Validator::make($data = $request->all(), [
                'start_time' => 'date_format:Y-m-d H:i:s',//页面
            ],[
                'start_time.date_format' => '开始时间格式错误，例：'.date('Y-m-d H:i:s')
            ]);

            //参数错误响应
            if ($infos = $timeValidator->errors()->first()) {
                return response()->json([
                    'code' => 1,
                    'msg' => $infos,
                    'data' => '',
                ]);
            }
        }

        if(isset($request->end_time)) {
            $timeValidator = Validator::make($data = $request->all(), [
                'end_time' => 'date_format:Y-m-d H:i:s',//页面
            ],[
                'end_time.date_format' => '结束时间格式错误，例：'.date('Y-m-d H:i:s')
            ]);

            //参数错误响应
            if ($infos = $timeValidator->errors()->first()) {
                return response()->json([
                    'code' => 1,
                    'msg' => $infos,
                    'data' => '',
                ]);
            }
        }

        $data['user_id'] = $this->getUserId();
        $data['main_id'] = $this->getMainUserId();

        if($this->getUserId() == $this->getMainUserId()){
            $userId['a.main_id'] = ['EQ' => $this->getMainUserId()];
        }
        else {
            //type类型：0-报溢 1-报损
            if($data['type']) {
                if($this->isShareBreakage()) {
                    $userId['a.main_id'] = ['EQ' => $this->getMainUserId()];
                }
                else {
                    $userId['a.user_id'] = ['EQ' => $this->getUserId()];
                }
            }
            else {
                if($this->isShareOverflow()) {
                    $userId['a.main_id'] = ['EQ' => $this->getMainUserId()];
                }
                else {
                    $userId['a.user_id'] = ['EQ' => $this->getUserId()];
                }
            }
        }

        return $this->success(
            OverflowBreakageService::list($data, $userId)
        );
    }

    /**
     * 报溢报损详细
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/5/24 17:18
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'id' =>'required|integer',//报溢报损主单 主键id
        ],[
            'id.required' => '报溢报损主单id缺失'
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail($info);
        }

        return $this->success(
            OverflowBreakageService::commonSearch('', $data)
        );
    }

    /**
     *报溢报损撤销
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/5/24 17:31
     * @return \Illuminate\Http\JsonResponse
     */
    public function repeal(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'id' =>'required|integer',//报溢报损主单 主键id
        ],[
            'id.required' => '报溢报损主单id缺失'
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail($info);
        }

        return response()->json(
            OverflowBreakageService::repeal($data)
        );
    }
}
